var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
  data: {},
  onLoad() {},
  /**
   * 提交登录信息
   */
  _submit(evt) {
    if (this._validate(evt.detail.value)) {
      var {
        account,
        password
      } = evt.detail.value;

      var parmasUrl = '/public/api/user/login';
      var paramsData = {
        mobile: account,
        pwd: password
      };
      ajax.P({
        url: parmasUrl,
        data: paramsData,
        success: (res) => {
          wx.setStorageSync('uid', res.data.uid);
          if (getCurrentPages().length == 1) {
            wx.reLaunch({
              url: '/pages/home/index',
            })
          } else {
            wx.navigateBack();
          }
        },
        fail: (res) => {}
      })
    }
  },
  /**
   * 验证输入信息
   */
  _validate(value) {
    var {
      account,
      password
    } = value;
    if (!account) {
      util.tips('账号不能为空！');
      return false;
    } else if (!password) {
      util.tips('密码不能为空！');
      return false;
    } else if (password.length < 8 || password.length > 16) {
      util.tips('密码格式不正确！');
      return false;
    }
    return true;
  }
}));